﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pst_Backup_Agent
{
    public partial class FrmShutdownComputer : Form
    {
        private Timer _timer = new Timer();
        private int _countdown = 60;
        private Utilities _logger = new Utilities();

        public FrmShutdownComputer(Utilities logger)
        {
            InitializeComponent();
            this._logger = logger;
            this._timer.Interval = 1000;
            this._timer.Tick += _timer_Tick;
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            this._countdown--;
            this.lblCountdown.Text = this._countdown.ToString();

            if (this._countdown <= 0)
            {
                this.Shutdown();
            }
        }

        private void Shutdown()
        {
            try
            {
                this._timer.Stop();
                this._logger.LogEvent(14, "The computer will shutdown now.", 1);
                System.Diagnostics.Process.Start("shutdown", "/p /f");
            }
            catch (Exception) { }
        }

        private void btnShutdownNow_Click(object sender, EventArgs e)
        {
            this.Shutdown();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this._logger.LogEvent(13, "The shutdown have been aborted.", 1);
            this._timer.Stop();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void FrmShutdownComputer_Shown(object sender, EventArgs e)
        {
            this._logger.LogEvent(12, "A shutdown is scheduled.", 1);
            this._timer.Start();
        }
    }
}
